package server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class ChatClientManual extends Thread {
    private static int clientsNumber;
    private static int n = 1234;

    public static synchronized int getClientsNumber() {
        return clientsNumber;
    }

    public void run() {
        clientsNumber++;
        try(final Socket socket = new Socket("localhost", 7777)) {

            new Thread() {
                private DataInputStream dis = new DataInputStream(socket.getInputStream());
                private String lastMessage;
                @Override public void run() {
                    while(true) {
                        try {
                            lastMessage = dis.readUTF();
                            System.out.println(lastMessage);
                        } catch (IOException e) {break;}
                    }
                }
            }.start();

            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            Scanner sc = new Scanner(System.in);
            //dos.writeUTF("/chid " + n++);
            while(true) {
                dos.writeUTF(sc.nextLine());
//                dos.writeUTF("/snd URGENT_MESSAGE");
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {}
            }

        } catch (IOException e) {}
        clientsNumber--;
    }

    public static void main(String[] args) {
        new ChatClientManual().start();
    }
}